{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1c234b28",
   "metadata": {},
   "source": [
    "## Reproject UVIS --> IR pixel scalings\n",
    "\n",
    "\n",
    "This notebook contains 2 cells:\n",
    "1. load imports and define main working directory\n",
    "2. loop over target directories, look for F200LP or F606W observations and if those exist, reproject them to any available F140W observation\n",
    "\n",
    "Last ran 2025.10.26 by C. Watson"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a66e9a58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, glob, h5py\n",
    "from astropy.io import fits\n",
    "from astropy.utils.data import get_pkg_data_filename\n",
    "import astropy.units as u\n",
    "from astropy.wcs import WCS\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from reproject.mosaicking import find_optimal_celestial_wcs, reproject_and_coadd\n",
    "from reproject import reproject_interp\n",
    "\n",
    "\n",
    "main_dir = '/Volumes/AGEL/HST_data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33175fab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Volumes/AGEL/HST_data/AGEL051603-220847A  reprojection of F606W -> F140W done\n"
     ]
    }
   ],
   "source": [
    "RENAME_FILES = True\n",
    "\n",
    "for target_dir in [d for d in glob.glob(os.path.join(main_dir, '*')) if os.path.isdir(d)]:\n",
    "    target = target_dir.split('/')[-1]\n",
    "\n",
    "    ref_fits1 = glob.glob('{}/HST/16773_F140W/*_drz_sci.fits'.format(target_dir))\n",
    "    ref_fits2 = glob.glob('{}/HST/15867_F140W/*_drz_sci.fits'.format(target_dir))\n",
    "\n",
    "    fits_file1 = glob.glob('{}/HST/16773_F200LP/*_drc_sci.fits'.format(target_dir))\n",
    "    fits_file2 = glob.glob('{}/HST/17307_F606W/*_drc_sci.fits'.format(target_dir))\n",
    "\n",
    "    ## -- If you have F200LP from Glazebrook, reproject it -- ##\n",
    "    if len(fits_file1) > 0:\n",
    "        ## -- If you have the F140W from Glazebrook use it as the reference image -- ##\n",
    "        if len(ref_fits1) > 0:\n",
    "            hdu1 = fits.open(ref_fits1[0])[0]\n",
    "            hdu2 = fits.open(fits_file1[0])[0]\n",
    "            array, footprint = reproject_interp(hdu2, hdu1.header)\n",
    "            fits.writeto(f'{target_dir}/HST/16773_F200LP/{target}_F200LP_WFC3_drc_img_scaled_L3.fits', array, hdu1.header, overwrite=True)\n",
    "            print(target_dir, ' reprojection of F200LP -> 16773 F140W done')\n",
    "\n",
    "        ## -- Else, If you have the F140W from Huang use it as the reference image -- ##\n",
    "        elif len(ref_fits2) > 0:\n",
    "            hdu1 = fits.open(ref_fits2[0])[0]\n",
    "            hdu2 = fits.open(fits_file1[0])[0]\n",
    "            array, footprint = reproject_interp(hdu2, hdu1.header)\n",
    "            fits.writeto(f'{target_dir}/HST/16773_F200LP/{target}_F200LP_WFC3_drc_img_scaled_L3.fits', array, hdu1.header, overwrite=True)\n",
    "            print(target_dir, ' reprojection of F200LP -> 15867 F140W done')\n",
    "\n",
    "    ## -- If you have F606W from Tran, reproject it -- ##\n",
    "    if len(fits_file2) > 0:\n",
    "        ## -- If you have the F140W from Glazebrook use it as the reference image -- ##\n",
    "        if len(ref_fits1) > 0:\n",
    "            hdu1 = fits.open(ref_fits1[0])[0]\n",
    "            hdu2 = fits.open(fits_file2[0])[0]\n",
    "            array, footprint = reproject_interp(hdu2, hdu1.header)\n",
    "            fits.writeto(f'{target_dir}/HST/17307_F606W/{target}_F606W_ACS_drc_img_scaled_L3.fits', array, hdu1.header, overwrite=True)\n",
    "\n",
    "                \n",
    "        ## -- Else, If you have the F140W from Huang use it as the reference image -- ##\n",
    "        elif len(ref_fits2) > 0:\n",
    "            hdu1 = fits.open(ref_fits2[0])[0]\n",
    "            hdu2 = fits.open(fits_file2[0])[0]\n",
    "            array, footprint = reproject_interp(hdu2, hdu1.header)\n",
    "            fits.writeto(f'{target_dir}/HST/17307_F606W/{target}_F606W_ACS_drc_img_scaled_L3.fits', array, hdu1.header, overwrite=True)\n",
    "        print(target_dir, ' reprojection of F606W -> F140W done')\n",
    "\n",
    "    if RENAME_FILES:\n",
    "        if len(ref_fits1) > 0:\n",
    "            hdu1 = fits.open(ref_fits1[0])[0]\n",
    "            fits.writeto(f'{target_dir}/HST/16773_F140W/{target}_F140W_WFC3_drz_img_L1.fits', hdu1.data, hdu1.header, overwrite=True)\n",
    "        if len(ref_fits2) > 0:\n",
    "            hdu1 = fits.open(ref_fits2[0])[0]\n",
    "            fits.writeto(f'{target_dir}/HST/15867_F140W/{target}_F140W_WFC3_drz_img_L1.fits', hdu1.data, hdu1.header, overwrite=True)\n",
    "        if len(fits_file1) > 0:\n",
    "            hdu1 = fits.open(fits_file1[0])[0]\n",
    "            fits.writeto(f'{target_dir}/HST/16773_F200LP/{target}_F200LP_WFC3_drc_img_L1.fits', hdu1.data, hdu1.header, overwrite=True)\n",
    "        if len(fits_file2) > 0:\n",
    "            hdu1 = fits.open(fits_file2[0])[0]\n",
    "            fits.writeto(f'{target_dir}/HST/17307_F606W/{target}_F606W_ACS_drc_img_L1.fits', hdu1.data, hdu1.header, overwrite=True)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "025036af",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "stenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
